// API - 系统用户

import { http } from "@/utils/http";
import { errorReject } from "@/utils/truncate";
import { message } from "@/utils/message";
import type { SysDeptInfo } from "./dept";
import type { SysRoleInfo } from "./role";

interface RolesResult extends HttpResponse {
  data?: SysRoleInfo[];
}

interface DeptsResult extends HttpResponse {
  data?: SysDeptInfo[];
}

interface ListResult extends HttpResponse {
  data?: {
    /** 列表数据 */
    list: SysUserInfo[];
    /** 总条目数 */
    total?: number;
    /** 每页显示条目个数 */
    pageSize?: number;
    /** 当前页数 */
    currentPage?: number;
  };
}

interface DetailResult extends HttpResponse {
  data?: SysUserInfo;
}

export type SysUserInfo = {
  id: Id;
  username: number;
  nickname: string;
  depts: SysDeptInfo[];
  roles: SysRoleInfo[];
  phone: string;
  email: string;
  status: boolean;
  sort: string | number;
  remark: string;
  createTime: string;
  updateTime: string;
};

/**
 * 获取角色列表
 *
 * @param params 请求参数
 * @returns
 */
export const getSysUserRoleList = async (params?: object) => {
  return errorReject(
    http.request<RolesResult>("get", "/system/users/roles", {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取部门列表
 *
 * @param params 请求参数
 * @returns
 */
export const getSysUserDeptsList = async (params?: object) => {
  return errorReject(
    http.request<DeptsResult>("get", "/system/users/depts", {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取分页列表
 *
 * @param data 请求参数
 * @returns
 */
export const getSysUserPageList = async (data?: object) => {
  return errorReject(
    http.request<ListResult>("post", "/system/users/page", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取信息
 *
 * @param id 请求ID
 * @param params 请求参数
 * @returns
 */
export const getSysUserDetail = (id: Id, params?: object) => {
  return errorReject(
    http.request<DetailResult>("get", "/system/users/" + id, {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 创建信息
 *
 * @param data 请求参数
 * @returns
 */
export const addSysUserDetail = (data?: object) => {
  return errorReject(
    http.request<DetailResult>("post", "/system/users", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysUserDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/users/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息（密码）
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysUserPassword = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/users/" + id + "/password", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息（角色）
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysUserRoles = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/users/" + id + "/roles", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息（状态）
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysUserStatus = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/users/" + id + "/status", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const delSysUserDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/users/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 批量删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const batchDelSysUserDetail = (data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/users", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};
